package priv.pront.code.lanqiao.competition.course.date0403;

public class 二分模板 {
    static int[] arr = {1, 2, 2,2, 9, 9, 9, 11, 15, 17, 19};
    static int target = 2, left, right;

    public static void main(String[] args) {
        bSearch_4();
    }

    //    查找元素的最小出现位置 [left,right)
    private static void bSearch_1() {
        left = 0;
        right = arr.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] >= target) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        System.out.println(left);
    }

    //    找目标值的最大位置 (left, right]
    private static void bSearch_2() {
        left = 0;
        right = arr.length - 1;
        while (left < right) {
            int mid = (left + right + 1) / 2;
            if (arr[mid] <= target) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        System.out.println(left);
    }
//  查找第一个大于 target 的值
    private static void bSearch_3() {
        left = 0;
        right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) /2;
             if(arr[mid] <= target){
                left = mid + 1;
            }else{
                right = mid - 1;
            }
        }
            System.out.println(left);
    }
//  查找第一个小于 target 的值
    private static void bSearch_4(){
        left = 0;
        right = arr.length -1;
        while (left <= right) {
            int mid = left + (right - left) /2;
            if(arr[mid] >= target){
                right = mid -1;
            }else{
                left = mid + 1;
            }
        }
        System.out.println(right);
    }
}
